package com.example.shirospringboot.serviceimpl;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.example.shirospringboot.mapper.SysUserMapper;
import com.example.shirospringboot.model.SysUser;
import com.example.shirospringboot.requestBean.User;
import com.example.shirospringboot.service.SysuserService;

@Component
public class SysuserServiceImpl implements SysuserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Value("${passwordHashIterations}")
    private  int passwordHashIterations;
    
    @Override
    @Transactional
    public void addUser(User user) {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(user.getUsername());
        sysUser.setRealName(user.getRealname());
        sysUser.setPassowrd(user.getPassword());
        sysUserMapper.insert(sysUser);
        int userid = sysUser.getUserid();
    
        /**
         * 使用md5进行加密，并且使用userid作为盐
         */
        String hashEdPassword = new Md5Hash(user.getPassword(), String.valueOf(userid), passwordHashIterations).toHex();
        sysUser = new SysUser();
        sysUser.setUserid(userid);
        sysUser.setPassowrd(hashEdPassword);
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
    
    }
}
